package com.anlogic.sdk.dumprestorememory.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import com.anlogic.sdk.dumprestorememory.DumpRestoreModel;
import com.anlogic.sdk.dumprestorememory.ui.DumpRestoreDialog;
import com.anlogic.sdk.dumprestorememory.utils.DumpRestoreMemoryUtils;

public class DumpRestoreMemoryHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		DumpRestoreDialog dialog = new DumpRestoreDialog(window.getShell());
		int ret = dialog.open();
		if (ret == DumpRestoreDialog.OK) {
			run(dialog.getModel());
		}
		return null;
	}

	private void run(DumpRestoreModel model) {
		if (model.isDump()) {
			DumpRestoreMemoryUtils.writeScriptDump(model);
			DumpRestoreMemoryUtils.dump(model);
		} else {
			DumpRestoreMemoryUtils.writeScriptRestore(model);
			DumpRestoreMemoryUtils.restore(model);
		}

	}

}
